package com.baichuan.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.baichuan.cons.ResponseCode;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @Auther: leo
 * @Date: 2020/04/10 13:05
 * @Description: 返回信息包装类
 */
public class ResponseResult {

    /**
     * 无返回值成功
     *
     * @return
     */
    public static String writeSuccess() {
        return writeSuccess(null, null);
    }

    /**
     * 指定key-value的返回值
     *
     * @param objectKey
     * @param objectValue
     * @return
     */
    public static String writeSuccess(String objectKey, Object objectValue) {
        return writeSuccess(objectKey, objectValue, null);
    }

    /**
     * 指定json对象
     *
     * @param m
     * @return
     */
    public static String writeSuccess(Map<? extends String, ? extends Object> m) {
        return writeSuccess(null, null, m);
    }

    /**
     * 无效请求
     *
     * @return
     */
    public static String writeInvalid() {
        return writeInvalid(null);
    }

    /**
     * 带调整信息的无效请求
     *
     * @param debugMsg
     * @return
     */
    public static String writeInvalid(String debugMsg) {
        return writeF("无效请求", ResponseCode.COMMON_ERROR, debugMsg);
    }

    /**
     * 自定义失败消息的返回值
     *
     * @param msg
     * @return
     */
    public static String writeFail(String msg) {
        return writeFail(msg, null);
    }

    /**
     * 自定义失败消息的返回值
     *
     * @param msg
     * @return
     */
    public static String writeFail(String msg, String debugMsg) {
        return writeF(msg, ResponseCode.COMMON_ERROR, debugMsg);
    }

    /**
     * 系统错误
     *
     * @return
     */
    public static String writeServerError() {
        return writeServerError(null);
    }

    /**
     * 系统错误
     *
     * @return
     */
    public static String writeServerError(String debugMsg) {
        return writeF("系统错误，请稍后再试", ResponseCode.SERVER_ERROR, debugMsg);
    }

    /**
     * 404错误
     *
     * @return
     */
    public static String writeFail404() {
        return writeF("请求资源不存在", ResponseCode.COMMON_ERROR);
    }

    /**
     * token验证失败
     *
     * @return
     */
    public static String writeTokenError() {
        return writeF("token验证失败", ResponseCode.TOKEN_ERROR);
    }

    /**
     * 用户被冻结
     *
     * @return
     */
    public static String writeFrozenAccountError() {
        return writeF("用户被冻结", ResponseCode.FROZEN_ACCOUNT_ERROR);
    }

    public static String writePermissionError() {
        return writeF("无操作权限", ResponseCode.PERMISSION_ERROR);
    }

    private static String writeF(String msg, String errCodee) {
        return writeF(msg, errCodee, null);
    }

    private static String writeF(String msg, String crrCode, String debugMsg) {
        return write(msg, ResponseCode.RTN_FAIL, crrCode, null, null, null, debugMsg);
    }

    private static String writeSuccess(String objectKey, Object objectValue, Map<? extends String, ? extends Object> m) {
        return write("ok", ResponseCode.RTN_SUCCESS, null, objectKey, objectValue, m, null);
    }


    private static String write(String msg, String rtn, String errCodee, String objectKey, Object objectValue, Map<? extends String, ? extends Object> m, String debugMsg) {
        JSONObject data = new JSONObject();
        data.put("rtn", rtn);
        data.put("timestamp", System.currentTimeMillis());
        if (msg != null)
            data.put("msg", msg);
        if (errCodee != null)
            data.put("errCode", errCodee);
        if (objectKey != null)
            data.put(objectKey, objectValue);
        if (m != null)
            data.putAll(m);
        if (debugMsg != null)
            data.put("debugMsg", debugMsg);
        return printJSON(data);
    }

    /**
     * 转为json字符串
     *
     * @param data
     * @return
     */
    private static String printJSON(JSONObject data) {
        return JSON.toJSONString(data, filter, SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.PrettyFormat,
                //SerializerFeature.WriteMapNullValue,
                //SerializerFeature.QuoteFieldNames,
                SerializerFeature.WriteDateUseDateFormat);
    }
    private static ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object object, String name, Object value) {
            if(value instanceof BigDecimal || value instanceof Double || value instanceof Float){
                return new BigDecimal(value.toString()).setScale(2, BigDecimal.ROUND_HALF_UP);
            }
            return value;
        }
    };

}
